Ext.BLANK_IMAGE_URL='/wsd/image/page/s.gif';
Ext.onReady(function(){


	
	var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([
		sm,
		{header:'编号',dataIndex:'id',width : 20},
		{header:'标题',dataIndex:'title'},
		{header:'地址',dataIndex:'url',renderer:function(value){
			return '<a href="' + value + '" target="_blank">'+ value + '</a>'
			
		}},
		{header:'添加日期',dataIndex:'addDate'}
	])
	
	var linkStore = new Ext.data.JsonStore({
		url:'/wsd/admin/link_allLinks.action',
		totalProperty:'result.total',
		root:'result.links',
		autoLoad:true,
		baseParams:{
			start:0,
			limit:10
		},
		fields:['id','title','url','addDate']
	})
	
	var linkPanel = new Ext.grid.GridPanel({
		region:'center',
		store:linkStore,
		viewConfig:{
			forceFit:true
		},
		loadMask:true,
		autoScroll:true,
		cm:cm,
		sm:sm,
		bbar:new Ext.PagingToolbar({
			pageSize:10,
			store:linkStore,
			displayInfo:true,
			displayMsg:'显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			emptyMsg:'没有记录'
		}),
		tbar:new Ext.Toolbar([
			{
				text:'添加',
				handler:function(){
					createLinkWindow();
				
				}
			},
			'->',
			{
				text:'删除',
				handler:function(){
					//var record = productsPanel.getSelectionModel().getSelected();
					var records = sm.getSelections();
					//var product = record.data;
					var len = records.length;
					if(records.length == 0){
						Ext.Msg.alert('系统信息','请先选要删除的链接');
						return false;
					}
					Ext.Msg.confirm('系统信息','你确定要删除这' + len + '条链接',function(btn){
						if(btn == 'yes'){
							del(records);
						}
					});
				}
			}
		])
	})
	
	linkPanel.on('rowdblclick',function(grid,rowIndex,e){
		//openOrderListWindow()
		var r = linkPanel.getSelectionModel().getSelected();
		if(r != null){
			createLinkWindow(r);
		}
		
	})
	//中间主面板
	var panel = new Ext.Panel({
		region:'center',
		layout:'border',
		items:[linkPanel]
	})
	
	
	var viewport = new Ext.Viewport({
		layout : 'border',
		frame:true,
		items : [panel]
	});

	function del(records){
		var len = records.length;
		var ids = new Array();
		for(var i = 0; i < len; i++){
			ids.push(records[i].data.id);
		}
		Ext.Msg.wait('正在删除，请稍后……');
		Ext.Ajax.request({
			url:'/wsd/admin/link_delete.action',
			params:{
				ids:ids
			},
			success:function(response){
				Ext.Msg.hide();
				linkStore.reload();
				Ext.Msg.alert('系统信息','删除成功!');
			},
			failure:function(response){
				Ext.Msg.hide();
				Ext.Msg.alert('系统信息','删除失败!');
			}
		})
	}
		
	function createLinkWindow(r){
		var modify = (r != null);
		var titleField = new Ext.form.TextField({
			fieldLabel:'标题'
		})
		var urlField = new Ext.form.TextField({
			fieldLabel : '地址',
			width : 400
		})
		var saveButton = new Ext.Button({
			text : '保存',
			handler : function(){
				if(!validate()){
					return false;
				}
				Ext.Msg.wait("正在保存……");
				save(titleField.getValue(),urlField.getValue());
					
			}
		})
		var modifyButton = new Ext.Button({
			text : '更新',
			handler : function(){
				if(!validate()){
					return false;
				}
				Ext.Msg.wait("正在更新……");
				update(r.data.id,titleField.getValue(),urlField.getValue());
					
			}
			
		})
		var linkWindow = new Ext.Window({
			title : '添加链接',
			layout : 'form',
			width : 500,
			height : 150,
			labelWidth : 50,
			constrain : true,
			modal : true,
			items : [titleField,urlField]
		})
		
		
		if(modify){
			titleField.setValue(r.data.title);
			urlField.setValue(r.data.url);
			linkWindow.addButton(modifyButton)
		}else{
			linkWindow.addButton(saveButton)
		}
		linkWindow.show();
		function validate(){
			if(titleField.getValue() == ''){
				Ext.Msg.alert('系统信息','链接标题不能为空');
				return false;
			}
			if(urlField.getValue() == ''){
				Ext.Msg.alert('系统信息','链接地址标题不能为空');
				return false;
			}
			return true;
		}
		
		function save(title,url){
			Ext.Ajax.request({
				url:'/wsd/admin/link_save.action',
				params:{
					'link.title':title,	
					'link.url':url
				},
				success:function(response){
					Ext.Msg.hide();
					linkWindow.close();
					linkStore.load();
					Ext.Msg.alert("系统信息","添加成功");
				},
				failure:function(response){
					Ext.Msg.hide();
					Ext.Msg.alert('系统消息','添加失败');
				}
			})
		}
		
		function update(id,title,url){
			Ext.Ajax.request({
				url:'/wsd/admin/link_update.action',
				params:{
					'link.id' : id,
					'link.title':title,	
					'link.url':url
				},
				success:function(response){
					Ext.Msg.hide();
					linkWindow.close();
					linkStore.load();
					Ext.Msg.alert("系统信息","更新成功");
				},
				failure:function(response){
					Ext.Msg.hide();
					Ext.Msg.alert('系统消息','更新失败');
				}
			})
		}
		
	
	}
	
});